package com.shop.seckill.service.impl;

import com.shop.seckill.service.SeckillIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class SeckillIndexServiceImpl implements SeckillIndexService {
    private final ConcurrentHashMap<String ,List> cache = new ConcurrentHashMap<>();
    @Autowired
    private RedisTemplate redisTemplate;
    // 查询秒杀商品
    @Override
    public Map<String, List> index() {
        Map<String, List> map = new HashMap<String, List>();
        Date date  = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 获取今天的日期 9 26
        String format = sdf.format(date.getTime());
        if(cache.containsKey(format)){
           // return
        }
        SimpleDateFormat s = new SimpleDateFormat("HH");
        Integer hour = Integer.valueOf(s.format(date));
        Set<String> members = redisTemplate.opsForSet().members(format);
        for (String string : members) {
            Integer i = Integer.valueOf(string.split(":")[0]);
            if(hour<i){
                List values = redisTemplate.boundHashOps(format + string).values();
                map.put(string, values);
            }
        }
        return map;
    }
}
